package net.cjw.initsb.service.impl;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import net.cjw.initsb.common.ServiceResult;
import net.cjw.initsb.dao.QuartzTaskMapper;
import net.cjw.initsb.entity.QuartzTask;
import net.cjw.initsb.entity.Role;
import net.cjw.initsb.service.QuartzTaskServiceI;
import net.cjw.initsb.utils.UUIDBuild;

@Service
@Transactional
public class QuartzTaskServiceImpl implements QuartzTaskServiceI {

	Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private QuartzTaskMapper quartzTaskMapper;

	@Override
	public ServiceResult<String> create(QuartzTask entity) {

		QuartzTask tmp;
		QuartzTask rs;
		tmp = new QuartzTask();
		tmp.setJobName(entity.getJobName());
		rs = quartzTaskMapper.selectOne(tmp);
		if (rs == null) {
			entity.setCreateTime(new Date());
			entity.setId(UUIDBuild.createUUId());
			entity.setIsPause(false);
			quartzTaskMapper.insertSelective(entity);
		}

		return ServiceResult.buildSuccess();
	}

	@Override
	public ServiceResult<String> update(QuartzTask entity) {
		quartzTaskMapper.updateByPrimaryKeySelective(entity);
		return ServiceResult.buildSuccess();
	}

	@Override
	public ServiceResult<String> delete(QuartzTask entity) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ServiceResult<QuartzTask> selectOne(QuartzTask entity) {
		QuartzTask tmp;
		QuartzTask rs;
		tmp = new QuartzTask();
		tmp.setJobName(entity.getJobName());
		rs = quartzTaskMapper.selectOne(tmp);
		if (rs == null) {
			create(entity);
			rs = entity;
		}
		return ServiceResult.buildSuccess(rs);
	}

	@Override
	public ServiceResult<Object> selectAll(Integer start, Integer max) {
		PageHelper page = new PageHelper();
		page.startPage(start, max);
		List<QuartzTask> list = quartzTaskMapper.selectAll();
		PageInfo<QuartzTask> pageInfo = new PageInfo<QuartzTask>(list);
		return ServiceResult.buildSuccess(list, pageInfo.getTotal());
	}

}
